//
//  CJCustomNavigationTitleBtnView.m
//  DatangLife
//
//  Created by 陈建 on 2018/4/30.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomNavigationTitleBtnView.h"

@implementation CJCustomNavigationTitleBtnView{
    NSArray *_titleArray;
    
    
}



- (instancetype)initWithFrame:(CGRect)frame withBtnTitleArray:(NSArray *)titleArray{
    self = [super initWithFrame:frame];
    if (self) {
        _titleArray = titleArray;
        [self setupTitleView];
    }
    return self;
}
- (void)setupTitleView{
    CGFloat btnW = self.width/_titleArray.count;
    CGFloat btnH = self.height;
    UIColor *selectBtnColor = kCommonRedColor;
    UIColor *nomalBtnColor = [UIColor colorWithHexString:kNomalTextBlackColor];
    NSMutableArray *titArr = [NSMutableArray array];
    for (NSInteger i = 0; i < _titleArray.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i * btnW, 0, btnW, btnH)];
        btn.tag = i;
        btn.titleLabel.font = kFont(16.f);
        [btn setTitleColor:selectBtnColor forState:UIControlStateSelected];
        [btn setTitleColor:nomalBtnColor forState:UIControlStateNormal];
        btn.adjustsImageWhenHighlighted = NO;
        if (i == 0) {
            btn.selected = YES;
            _selectBtn = btn;
        }
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [titArr addObject:btn];
    }
    self.btnArray = titArr;
}

- (void)btnClickAction:(UIButton *)btn{
    NSInteger tag = btn.tag;
 
    
    if (tag != _selectBtn.tag) {
        _selectBtn.selected = NO;
        _selectBtn = btn;
        _selectBtn.selected = YES;
        NSLog(@"%ld",tag);
        if ([self.delegate respondsToSelector:@selector(tapNavigationTitleBtnClickAction:)]) {
            [self.delegate tapNavigationTitleBtnClickAction:tag];
        }
    }
    
    
    
}


@end
